package wassalt.Utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import wassalt.Services.HTTPService;
import wassalt.client.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.util.Log;

public class Util {

	private static final String TAG = "Util";

//	/**
//	 * Cache containing the base URL for a given context.
//	 */
//	private static final Map<Context, String> URL_MAP = new HashMap<Context, String>();
//
//	public static boolean isDebug(Context context) {
//		// Although this is a bit roundabout, it has the nice side effect
//		// of caching the result.
//		return !context.getString(R.string.server_url).equals(
//				getBaseUrl(context));
//	}
//
//	/**
//	 * Returns a debug url, or null. To set the url, create a file
//	 * {@code assets/debugging_prefs.properties} with a line of the form
//	 * 'url=http:/<ip address>:<port>'. A numeric IP address may be required in
//	 * situations where the device or emulator will not be able to resolve the
//	 * hostname for the dev mode server.
//	 */
//	private static String getDebugUrl(Context context) {
//		BufferedReader reader = null;
//		String url = null;
//		try {
//			AssetManager assetManager = context.getAssets();
//			InputStream is = assetManager.open("debugging_prefs.properties");
//			reader = new BufferedReader(new InputStreamReader(is));
//			while (true) {
//				String s = reader.readLine();
//				if (s == null) {
//					break;
//				}
//				if (s.startsWith("url=")) {
//					url = s.substring(4).trim();
//					break;
//				}
//			}
//		} catch (FileNotFoundException e) {
//			// O.K., we will use the production server
//			return null;
//		} catch (Exception e) {
//			Log.w(TAG, "Got exception " + e);
//			Log.w(TAG, Log.getStackTraceString(e));
//		} finally {
//			if (reader != null) {
//				try {
//					reader.close();
//				} catch (IOException e) {
//					Log.w(TAG, "Got exception " + e);
//					Log.w(TAG, Log.getStackTraceString(e));
//				}
//			}
//		}
//
//		return url;
//	}
//
//	/**
//	 * Returns the (debug or production) URL associated with the registration
//	 * service.
//	 */
//	public static String getBaseUrl(Context context) {
//		String url = URL_MAP.get(context);
//		if (url == null) {
//			// if a debug_url raw resource exists, use its contents as the url
//			url = getDebugUrl(context);
//			// otherwise, use the production url
//			if (url == null) {
//				url = context.getString(R.string.server_url);
//			}
//			URL_MAP.put(context, url);
//		}
//		return url;
//	}
//
//	public static void getFulfilledNotification(Context context) {
//		String phone = User.registeredPhone(context);
//		// fire intent to http service
//		Intent regServiceIntent = new Intent(context, HTTPService.class);
//		regServiceIntent.setAction(AppConstants.REGISTRATION_RESPONSE_FILTER);
//		regServiceIntent.putExtra(AppConstants.URL,
//				context.getString(R.string.server_url)
//						+ AppConstants.GETARRIVALS_URL);
//		regServiceIntent.putExtra(AppConstants.PHONE, phone);
//		context.startService(regServiceIntent);
//	}

	/**
	 * Used to save variable indicating existing of connection
	 * 
	 * @param context
	 * @param key
	 *            a unique key to save the variable and fetch it using this key
	 * @author mhm
	 */
	public static void setConnecting(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS_CON, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.putBoolean(key + "Con", true);
		editor.commit();
	}

	/**
	 * sets the connecting variable to false and may be any extra info
	 * 
	 * @param context
	 * @param key
	 *            a unique key to save the variable and fetch it using this key
	 * @param extra
	 *            any extra information about the finish of connection is it
	 *            smooth or not if success or failure in order to act
	 *            accordenlly
	 * @author mhm
	 */
	public static void setDisconnecting(Context context, String key,
			String extra) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS_CON, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.putBoolean(key + "Con", false);
		if(extra !=null){
		editor.remove(key + "Con_extra");
		editor.putString(key + "Con_extra", extra);
		}
		editor.commit();
	}

	/**
	 * checks if this key connection is active or not.
	 * 
	 * @param context
	 * @param key
	 *            a unique key per variable to save the variable and fetch it
	 *            using this key
	 * @return returns true if the connection is on and false if there is no
	 *         connection active
	 * @author mhm
	 */
	public static boolean isConnecting(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS_CON, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(key + "Con", false);
		// SharedPreferences.Editor editor = sharedPreferences.edit();
		// editor.remove(key + "Con");
		// editor.commit();
		// return res;
	}

	/**
	 * to get the extra saved in case of disconnecting may be indicating the
	 * reason behind disconnecting if success or failure.
	 * 
	 * @param context
	 * @param key
	 *            a unique key per variable to save the variable and fetch it
	 *            using this key
	 * @return null in case of no info saved and String other wise
	 */
	public static String getExtra(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS_CON, Context.MODE_PRIVATE);
		return sharedPreferences.getString(key + "Con_extra", null);
		
	}
	
	public static void clearConnection(Context context, String key){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS_CON, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(key + "Con");
		editor.remove(key + "Con_extra");
		editor.commit();
	}

}
